//
//  RegisterViewController.m
//  SunXi
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 大远. All rights reserved.
//
#define ALLNIL _passField.text = nil; _TpassField.text = nil; _nameField.text = nil;

#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController{
    UILabel *_namelabel;
    UILabel *_passlabel;
    UILabel *_Tpasslabel;
    UITextField *_nameField;
    UITextField *_passField;
    UITextField *_TpassField;
    UIView *_nameXian;
    UIView *_passXian;
    UIView *_TpassXian;
}

- (void)viewDidLoad {
    [super viewDidLoad];
        self.navigationItem.title = @"注册";
    SV.BC = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    //MARK:登陆填写区
    [self initloadValue];
    
}
/**
 *  登陆信息区
 */
- (void)initloadValue{
    CGFloat hei = HEIGHT * 0.15;
    _namelabel = [[UILabel alloc]init];
    [SV addSubview:_namelabel];
    [_namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.top.equalTo(SV).offset(hei);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(80);
    }];
    _namelabel.text = @"账        号:";
    
    _nameField = [[UITextField alloc]init];
    [SV addSubview:_nameField];
    [_nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_namelabel.mas_right).offset(5);
        make.right.equalTo(SV).offset(-40);
        make.height.mas_equalTo(20);
        make.top.equalTo(_namelabel);
    }];
    _nameField.placeholder = @"请输入账号";
    _nameXian = [[UIView alloc]init];
    [SV addSubview:_nameXian];
    _nameXian.BC = BUTTONCOLOR;
    [_nameXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.equalTo(_namelabel.mas_bottom).offset(5);
        make.left.equalTo(_namelabel);
        make.right.equalTo(_nameField);
    }];
    
    
    //密码
    _passlabel = [[UILabel alloc]init];
    [SV addSubview:_passlabel];
    [_passlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.top.equalTo(_nameXian.mas_bottom).offset(25);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(80);
    }];
    _passlabel.text = @"密        码:";
    
    _passField = [[UITextField alloc]init];
    [SV addSubview:_passField];
    [_passField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_passlabel.mas_right).offset(5);
        make.right.equalTo(SV).offset(-40);
        make.height.mas_equalTo(20);
        make.top.equalTo(_passlabel);
    }];
    _passField.secureTextEntry = YES;
    _passField.placeholder = @"请输入密码";
    _passXian = [[UIView alloc]init];
    [SV addSubview:_passXian];
    _passXian.BC = BUTTONCOLOR;
    [_passXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.equalTo(_passlabel.mas_bottom).offset(5);
        make.left.equalTo(_passlabel);
        make.right.equalTo(_passField);
    }];
    
    
    //重复密码
    _Tpasslabel = [[UILabel alloc]init];
    [SV addSubview:_Tpasslabel];
    [_Tpasslabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.top.equalTo(_passlabel.mas_bottom).offset(25);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(80);
    }];
    _Tpasslabel.text = @"重复密码:";
    
    _TpassField = [[UITextField alloc]init];
    [SV addSubview:_TpassField];
    [_TpassField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_Tpasslabel.mas_right).offset(5);
        make.right.equalTo(SV).offset(-40);
        make.height.mas_equalTo(20);
        make.top.equalTo(_Tpasslabel);
    }];
    _TpassField.secureTextEntry = YES;
    _TpassField.placeholder = @"请输入密码";
    
    _TpassXian = [[UIView alloc]init];
    [SV addSubview:_TpassXian];
    _TpassXian.BC = BUTTONCOLOR;
    [_TpassXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.top.equalTo(_Tpasslabel.mas_bottom).offset(5);
        make.left.equalTo(_Tpasslabel);
        make.right.equalTo(_TpassField);
    }];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [SV addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SV).offset(40);
        make.right.equalTo(SV).offset(-40);
        make.centerY.equalTo(SV);
        make.height.mas_equalTo(30);
    }];
    [button setTitle:@"注册" forState:UIControlStateNormal];
    button.tintColor = [UIColor whiteColor];
    button.BC = BUTTONCOLOR;
    button.layer.cornerRadius = 5;
    [button addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    
}
/**
 *  注册实现方法
 */
- (void)registerClick{
    if (!_nameField.text.length || !_passField.text.length) {
        [ShowMytext showHUDTextWithTitle:@"用户名或密码为空" View:SV];
    }else if (![_passField.text isEqualToString:_TpassField.text]){
        [ShowMytext showHUDTextWithTitle:@"两次密码不一样" View:SV];
    }else{
        [ShowMytext showHUDloadView:SV text:@"注册中..."];
        [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:_nameField.text password:_passField.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
            [ShowMytext hiddHUDloadView];
            if (!error) {
                NSLog(@"注册成功");
                [ShowMytext showAlertViewTitlt:@"恭喜" message:@"注册成功" selector:self Block:^{
                    self.Block(_nameField.text,_passField.text);
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }else{
                switch (error.errorCode) {
                    case EMErrorServerDuplicatedAccount: {
                        [ShowMytext showAlertViewTitlt:@"失败" message:@"用户名重复" selector:self Block:^{
                            ALLNIL;
                        }];
                        break;
                    }
                    case EMErrorInvalidUsername_Chinese: {
                        [ShowMytext showAlertViewTitlt:@"失败" message:@"用户名不可以是中文" selector:self Block:^{
                            ALLNIL;
                        }];
                        break;
                    }
                    case EMErrorCallChatterOffline: {
                        [ShowMytext showAlertViewTitlt:@"失败" message:@"密码不能是中文" selector:self Block:^{
                            ALLNIL;
                        }];
                        break;
                    }
                    default: {
                        [ShowMytext showAlertViewTitlt:@"失败" message:@"请重新尝试" selector:self Block:^{
                            ALLNIL;
                        }];
                        break;
                    }
                }
            }
        } onQueue:nil];
    }
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)presentLeftMenuViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
